package com.yan.demo;

import java.util.Arrays;

// 选择排序
public class Test14 {
    public static void main(String[] args) {
        int[] arr = {5, 12, 90, 18, 77, 76, 45, 28, 59, 72};
        int[] arr1 = {5, 12, 90, 18, 77, 76, 45, 28, 59, 72};
        arr = selectionSortMin(arr);
        arr1 = selectionSortMax(arr1);
        System.out.println(Arrays.toString(arr));
        System.out.println(Arrays.toString(arr1));
    }

    public static int[] selectionSortMin(int[] arr) {
        int min, index;
        for (int i = 0; i < arr.length; i++) {
            min = arr[i];
            index = i;
            for (int j = i; j < arr.length; j++) {
                if (arr[j] < min) {
                    min = arr[j];
                    index = j;
                }
            }
            int temp = arr[i];
            arr[i] = arr[index];
            arr[index] = temp;
        }
        return arr;
    }

    public static int[] selectionSortMax(int[] arr) {
        int max, index;
        for (int i = 0; i < arr.length; i++) {
            max = arr[i];
            index = i;
            for (int j = i; j < arr.length; j++) {
                if (arr[j] > max) {
                    max = arr[j];
                    index = j;
                }
            }
            int temp = arr[i];
            arr[i] = arr[index];
            arr[index] = temp;
        }
        return arr;
    }
}
